<?php
// 应用公共文件

//curl post提交
function json_post($url, $data = NULL)
{

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    if (!$data) {
        return 'data is null';
    }
    if (is_array($data)) {
        $data = json_encode($data);
    }
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt(
        $curl,
        CURLOPT_HTTPHEADER,
        array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length:' . strlen($data),
            'Cache-Control: no-cache',
            'Pragma: no-cache'
        )
    );
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $res = curl_exec($curl);
    $errorno = curl_errno($curl);
    if ($errorno) {
        return $errorno;
    }
    curl_close($curl);
    return $res;
}


function json_get($url)
{

    $ch = curl_init(); // 初始化crul
    curl_setopt($ch, CURLOPT_URL, $url); // 抓取指定网页
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 要求结果为字符串并输出到屏幕上
    curl_setopt($ch, CURLOPT_HEADER, 0); // 设置header

    $output = curl_exec($ch); // 执行并获得HTML内容

    curl_close($ch); // 释放curl句柄
    return $output;
}
